package org.ricks.condition2;


import org.ricks.condition2.event.PlayerAwareEvent;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;

public interface Condition {

    void emit(PlayerAwareEvent event);

    boolean isDone();

    String getPlayerId();

    // 返回条件关注的事件类型，用于自动注册
    <E extends PlayerAwareEvent> Collection<Class<? extends E>> getInterestedEventTypes();

    // 通用比较方法，支持任意类型和比较器
    default <T> boolean compare(T current, T target, Comparator<T> comparator, ComparisonOperator operator) {
        int result = comparator.compare(current, target);
        return operator.test(result);
    }


    default void onRegistered() {} // 注册到Manager时触发
    default void onUnregistered() {} // 注销时触发
    default void onCompleted() {} // 完成时触发（在isDone变为true后）

    // 当关注的事件类型变化时返回true
    default boolean checkEventTypesChanged() {
        return false;
    }

    default Condition and(Condition other) {
        return new CompositeCondition(
                this.getPlayerId(),
                List.of(this, other),
                list -> list.stream().allMatch(Condition::isDone)
        );
    }

    default Condition or(Condition other) {
        return new CompositeCondition(
                this.getPlayerId(),
                List.of(this, other),
                list -> list.stream().anyMatch(Condition::isDone)
        );
    }
}
